<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Tracks extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->data = array();
		$this->load->model('trackmodel');
	}

	/**
	 * Index Page for this controller.  Should return nothing.
	 *
	 */
	public function index()
	{
	
	}
	
	public function reset_database(){
		$this->trackmodel->reset_database();
	}

	function _sendData(){
		$this->output->set_header("Content-type: application/json");
		$this->output->set_output(json_encode($this->data));
	}

	public function get_url($track_id){
		$this->output->set_header("Content-type: text/plain");
		$this->output->set_output( base_url( "tracks/".$this->trackmodel->get_track_url( $track_id ) ) );
	}

	/**
	 * Gets either the full list of tracks or just one as JSON.
	 *
	 */
	public function get($track_id=false) {
		if($track_id){
			// TODO: Handle retrieving all information for a single track.
					$this->data = $this->trackmodel->get_track($track_id);
					//$this->data = (object) $this->data;
		} else {
			// TODO: Handle retrieving all information for all available tracks.
			$rp         = $this->input->get_post('page',TRUE,15); // The # of rows per page.
			$page       = $this->input->get_post('page',TRUE,1); // The current page
			$sortname   = $this->input->get_post('sortname',TRUE,'id'); // Sort columns
			$sortorder  = $this->input->get_post('sortorder',TRUE,'asc'); // Sort order
			$qtype      = $this->input->get_post('qtype',TRUE,''); // Search column
			$query      = $this->input->get_post('query',TRUE,''); // Search string
			
			$this->data = $this->trackmodel->get_tracks($rp,$page,$sortname,$sortorder,$qtype,$query,true);
		}
		$this->_sendData();
	}

	public function add(){
		// TODO: Handle adding a track to the database.
		//print_r($_FILES);
		$upload_config['upload_path'] = __DIR__.'/../../../tracks/uploaded/';
		$upload_config['file_name'] = sha1( time() . rand() ).".mp3";
		$upload_config['allowed_types'] = 'mp3';
		$upload_config['max_size']	= '10240';
		$this->load->library('upload', $upload_config);
		
		$title = $this->input->get_post('title',TRUE,FALSE);
		$artists = $this->input->get_post('artists',TRUE,FALSE);
		
		if ( ! $this->upload->do_upload("file")) {
			$this->data['status']= "fail";
			$this->data['error'] = $this->upload->display_errors("","");
			
		} elseif (!$title || !$artists) {
			$this->data['status']= "fail";
			$this->data['error'] = (!$title?"Title not specified.\r\n":"").(!$artists?"Artists not specified":"");
		} else {
			$data = $this->upload->data();
			$file_name = $data['file_name'];
			$this->trackmodel->add_track($title,$artists,$file_name);
			$this->data['status']= "success";
			$this->data['debug'] = $this->upload->data();
		}
		$this->_sendData();
	}

	public function edit($track_id=false){
		// TODO: Handle editing a track in the database.
		$track_id = $this->input->get_post('track_id',TRUE,$track_id);
		$title    = $this->input->get_post('title',TRUE,FALSE);
		$artists  = $this->input->get_post('artists',TRUE,FALSE);
		
		if(!$this->trackmodel->edit_track($track_id, $title, $artists)){
			$this->data['status'] = "fail";
			$this->data['error'] = $this->trackmodel->err_msg;
		}
		else {
			$this->data['status'] = "success";
		}
		$this->_sendData();
	}

	public function remove($track_id=false) {
		$track_id = $this->get_post('track_id',TRUE,$track_id);
		if($track_id!=false){
			if(!$this->trackmodel->remove($track_id)){
				$this->data['status'] = "fail";
				if($this->trackmodel->err_msg){
					$this->data['error'] = $this->trackmodel->err_msg;
				} else {
					$this->data['error'] = "An unknown error has occurred while removing track.";
				}
				
			} else {
				$this->data['status'] = "success";
			}
		}
		$this->_sendData();
	}
}

/* End of file tracks.php */
/* Location: ./application/controllers/ajax/tracks.php */
